package m.jclecai.com.fragment.follow;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.Response.ResHotOrederDetail;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.ScreenUtil;

/**
 * Created by Dinosa on 2017/11/18.
 * 跟单详情的detail
 */

/**
 * Created by Dinosa on 2017/11/18.
 * 跟单详情的detail
 */

public class HotOrderDetailFragment extends BaseFragment {

    @BindView(R.id.tv_title)
    TextView mTvTitle;

    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;

    @BindView(R.id.table)
    TableLayout mTableLayout;

    @BindView(R.id.tv_content)
    TextView mTvContent;

    @BindView(R.id.tv_bet_lock_dec)
    TextView mTvBetLockDec;

    @BindView(R.id.ll_last)
    View mViewLast;

    @BindView(R.id.rlLock)
    View rlLock;

    private ResHotOrederDetail mHotOrederDetail;
    private boolean isSecret  = false;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot_order_detail;
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initView() {
        super.initView();
    }

    /**
     * 更新页面
     *
     * @param hotOrederDetail 这几串几，我们是不知道的所以这里我们是需要这样写的；
     */
    public void updateUI(ResHotOrederDetail hotOrederDetail) {


        mTvTitle.setText("过关方式: " + hotOrederDetail.getMethodname().replace("*", "串"));

        mTvEndTime.setText("截止时间: " + hotOrederDetail.getEndtime());
        mHotOrederDetail = hotOrederDetail;

//        if (mHotOrederDetail.getOflag() == 2) {
//            mViewLast.setVisibility(View.VISIBLE);
//            mTvContent.setVisibility(View.GONE);
//            //这里我们是需要判断一下是否只有一个条目，如果只有一个条目我们就隐藏内容；
//            if (mHotOrederDetail.getMatchorder().size()==1) {
//                mTvBetLockDec.setVisibility(View.GONE);
//            }else {
//                mTvBetLockDec.setVisibility(View.VISIBLE);
//            }
//        }else{
//            mViewLast.setVisibility(View.GONE);
//
//        }
        updateTable(hotOrederDetail);
    }

    /**
     * 这里我们用来更新表格里面的内容；
     */
    private void updateTable(ResHotOrederDetail hotOrederDetail) {
        int height = 0;
        int top = 0;
        int splitHeight = ScreenUtil.dip2px(getActivity(), 1);
        for (int i = 0; i < mHotOrederDetail.getMatchorder().size(); i++) {
            ResHotOrederDetail.MatchorderBean matchorderBean = mHotOrederDetail.getMatchorder().get(i);
            View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.table_row_layout, mTableLayout, false);
            mTableLayout.addView(inflate);

            TextView one = (TextView) inflate.findViewById(R.id.tv_one);
            TextView two = (TextView) inflate.findViewById(R.id.tv_two);
            TextView three = (TextView) inflate.findViewById(R.id.tv_three);
            TextView four = (TextView) inflate.findViewById(R.id.tv_four);
            TextView five = (TextView) inflate.findViewById(R.id.tv_five);

            one.setText(getOneText(matchorderBean));
            two.setText(Html.fromHtml(getTwoText(matchorderBean)));
            three.setText(Html.fromHtml(getThreeText(matchorderBean)));
            four.setText(Html.fromHtml(getFourText(matchorderBean)));
            one.measure(0,
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            //根据score有无值来判断是否显示开奖  matchSelect显示开奖数据
            if (isCanWatch(hotOrederDetail)) {
                five.setVisibility(View.VISIBLE);
                five.setText(Html.fromHtml(getFiveText(matchorderBean)));
                top += one.getMeasuredHeight() + splitHeight;
            } else {
                five.setVisibility(View.INVISIBLE);
            }
//            View.inflate(getActivity(),R.layout.table_row_line_layout,mTableLayout);
            height += one.getMeasuredHeight() + splitHeight;
        }

        //这里控制覆盖在开奖信息上的锁的位置大小
        rlLock.post(new Runnable() {
            @Override
            public void run() {

            }
        });
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlLock.getLayoutParams();
        layoutParams.topMargin = top;
        layoutParams.height = height - top;
        rlLock.setLayoutParams(layoutParams);
        if (isSecret)
            mTvBetLockDec.setText("保密单");
        else
            mTvBetLockDec.setText("截止后公开");
        if (top == height)
            rlLock.setVisibility(View.GONE);
    }



    /**
     * 这里表示是否可以看到保密；
     * @return
     */
    public boolean isCanWatch(ResHotOrederDetail hotOrederDetail){
        //
        if(hotOrederDetail.getOflag() == 0){
            //公开；
            return true;
        }else if(hotOrederDetail.getOflag() == 1){
            //截止后公开
            String endtime = hotOrederDetail.getEndtime();
            Date parse =null;
            try {
                parse = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(endtime);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            Date date = new Date();

            //这里表示截至时间已经开始了；
            if(parse.getTime()<date.getTime()){

                return true;
            }else{
                return false;
            }

        }else{
            //这里表示保密；
            UserBean userBean = PreferenceUtil.readUser(getActivity());
            if(userBean == null){

                return false;

            }else{
                //判断是不是自己，已经跟单里面有没有自己；

                String user_id = hotOrederDetail.getUser_id();
                List<String> follow_users = hotOrederDetail.getFollow_users();

                if(userBean.getUser_id().equals(user_id) ){
                    isSecret = false;
                    return true;
                }

                if(follow_users!=null && follow_users.size()>0){
                    boolean contains = follow_users.contains(userBean.getUser_id());
                    if (contains)
                        isSecret = false;
                    else
                        isSecret = true;

                    return  contains;
                }

            }

        }

        return false;
    }

    //这里是场次的数据源
    private String getOneText(ResHotOrederDetail.MatchorderBean bean) {

        return weekIntToStr(bean.getWeek()) + "\n" + getTerm(bean.getId());

    }

    //主队
    private String getTwoText(ResHotOrederDetail.MatchorderBean bean) {
        //这里我们做一个操作：
        if (!TextUtils.isEmpty(bean.getBall())) {

            //这里表示有让球的概念：
            double integer = 0;
            try {
                integer = Double.parseDouble(bean.getBall());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            if (integer >= 0) {

                return bean.getHome() + "<br/>" + createHtml("(" + bean.getBall() + ")", "red");
            } else if (integer < 0) {

                return bean.getHome() + "<br/>" + createHtml("(" + bean.getBall() + ")", "#003300");
            }
        }
        return bean.getHome();
    }

    //比分
    private String getThreeText(ResHotOrederDetail.MatchorderBean bean) {

        String socre = "";
        if (!TextUtils.isEmpty(bean.getHscore())) {
            socre += bean.getHscore();
        }
        if (!TextUtils.isEmpty(bean.getScore())) {
            if (!TextUtils.isEmpty(socre)) {
                socre += "\n";
            }
            socre += bean.getScore();
        }

        return socre;
    }

    //客队
    private String getFourText(ResHotOrederDetail.MatchorderBean bean) {
        return bean.getVisit();
    }

    //投注内容
    private String getFiveText(ResHotOrederDetail.MatchorderBean bean) {
        //这里是投注内容的部分；
        String content = "";
        //这里我们要设置
        for (int i = 0; i < bean.getMatchSelect().size(); i++) {

            boolean isAward = false;
            String result = bean.getMatchResult().get(i);

            result = result.split("\\(")[0];

            String select = bean.getMatchSelect().get(i);
            select = select.split("\\(")[0];

            if (result.equals(select) && !TextUtils.isEmpty(result)) {
                isAward = true;
            }

            String color = isAward ? "red" : "gray";
            content += createHtml(bean.getMatchSelect().get(i), color);
            if (i < bean.getMatchSelect().size() - 1) {
                content += "<br/>";
            }
        }
        return content;
    }

/*    public class HotOrderDeailAdpater extends BaseRecyclerViewAdapter<ResHotOrederDetail.MatchorderBean> {

        public HotOrderDeailAdpater(List<ResHotOrederDetail.MatchorderBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_fragment_hot_order_detail;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, ResHotOrederDetail.MatchorderBean bean, int position) throws ParseException {



            String screening =weekIntToStr(bean.getWeek()) +"\n" + getTerm(bean.getId());
            String mainTeam = bean.getHome() + createHtml("(" + bean.getBall() + ")", "red");


            String socre = "";

            //这里要写，全场和半全场；

            if(!TextUtils.isEmpty(bean.getHscore())){
                socre+=bean.getHscore();
            }
            if(!TextUtils.isEmpty(bean.getScore())){
                if(!TextUtils.isEmpty(socre)){
                    socre+="\n";
                }
                socre+=bean.getScore();
            }

            String content = "";
            if (mHotOrederDetail.getOflag() == 2) {
                content="截止后公开";
                holder.getTextView(R.id.tv_bet_content).setVisibility(View.GONE);
            }else{
                holder.getTextView(R.id.tv_bet_content).setVisibility(View.VISIBLE);
            //这里我们要设置
                for (int i = 0; i < bean.getMatchSelect().size(); i++) {

                    boolean isAward=false;
                    String result = bean.getMatchResult().get(i);

                    result=result.split("\\(")[0];

                    String select = bean.getMatchSelect().get(i);
                    select=select.split("\\(")[0];

                    if (result.equals(select) && !TextUtils.isEmpty(result)) {
                        isAward=true;
                    }


                    String color=isAward?"red":"gray";
                    content+=createHtml(bean.getMatchSelect().get(i),color);
                    if(i<bean.getMatchSelect().size()-1){
                        content+="<br/>";
                    }
                }
            }

            String visit = bean.getVisit();

            holder.getTextView(R.id.tv_screenings).setText(screening);
            holder.getTextView(R.id.tv_main_team).setText(Html.fromHtml(mainTeam));
            holder.getTextView(R.id.tv_score).setText(Html.fromHtml(socre));
            holder.getTextView(R.id.tv_visit).setText(visit);
            holder.getTextView(R.id.tv_bet_content).setText(Html.fromHtml(content));

            holder.getView(R.id.ll_item).setBackgroundResource(R.color.white);
        }
    }*/

    public String createHtml(String content, String color) {
        return "<html><font color='" + color + "'>" + content + "</font></html>";
    }

    /**
     * 将1，2，3，..转换对应的周日；
     *
     * @return
     */
    public String weekIntToStr(String week) {
        switch (week) {
            case "1":
                return "周一";
            case "2":
                return "周二";
            case "3":
                return "周三";
            case "4":
                return "周四";
            case "5":
                return "周五";
            case "6":
                return "周六";
            case "7":
                return "周日";
        }
        return "周一";
    }

    /**
     * 获取期号
     *
     * @return
     */
    public String getTerm(String term) {

        if (term.length() > 3) {
            return term.substring(term.length() - 3);
        } else {
            return term;
        }
    }

}
